import React, { useReducer } from 'react'

let initialValue = {
  num: 0
}

let reducer = function reducer(state, action) {
  state = { ...state }
  switch (action.type) {
    case 'plus':
      state.num++;
      break;
    case 'minus':
      state.num--;
      break;
    default:
  }
  return state
}

export default function TestUseReducer() {
  const [state, dispatch] = useReducer(reducer, initialValue)

  return <div>
    <div>count: {state.num}</div>
    <button onClick={() => {
      dispatch({ type: 'plus' })
    }}>新增</button>
    <button onClick={() => {
      dispatch({ type: 'minus' })
    }}>减少</button>
  </div>
}